<?php
/**
 * spb weather专用格式
 */
class _interface extends __interface {
	
	private $zwDirectionMap = array(
			'暂缺' => 0,
			'最弱' => 1,
			'弱' => 2,
			'中等' => 3,
			'强' => 4,
			'很强' => 5,
	);
	
	private $weatherDirectionMap = array(
			'晴' => '2',		// 晴
			'多云' => '3',		// 多云
			'晴间多云' => '3',
			'阴' => '5',		// 阴
			'有雨' => '14',
			'阵雨' => '14',		// 阵雨
			'雷阵雨' => '13',		// 雷阵雨
			'雷阵雨' => '13',		// 雷阵雨
			'6' => '9',		// 雨夹雪
			'小雨' => '7',		// 雨1
			'小到中雨' => '7',		// 雨2
			'中雨' => '7',		// 雨3
			'中到大雨'  => '8',		// 雨4
			'大雨'  => '8',	// 雨5
			'暴雨'  => '8',	// 雨6
			'阵雪' => '10',	// 阵雪
			'小雪' => '10',	// 雪1
			'中雪' => '10',	// 雪2
			'大雪' => '11',	// 雪3
			'暴雪' => '11',	// 雪4
			'雾' => '6',		// 雾
			'冻雨' => '7',		// 冻雨
			'20' => '6',		// 沙尘
			'21' => '6',		// 雨2
			'22'  => '8',	// 雨3
			'23'  => '8',	// 雨4
			'24' => '9',		// 雨5
			'25' => '9',		// 雨6
			'26' => '9',		// 雪2
			'27' => '9',		// 雪3
			'28' => '9',		// 雪4
			'29' => '6',		// 沙尘
			'30' => '6',		// 沙尘暴
			'31' => '6',		// 龙卷风
	);
	private $windDirectionMap = array(
			"南" => "S",
			"北" => "N",
			"东" => "E",
			"西" => "W",
			"东南" => "SE",
			"西南" => "WS",
			"东北" => "NE",
			"西北" => "NW",
			"微" => "N",
			"旋转" => "N",
			"无持续" => "N",
	);
	
	public function dispose_data() {
		
		$retarr = array();
		
		/********************************************* 今天天气数据 *********************************************/
		// 处理日期
		$rq = $this->weather_data["update_time"]["day"];
		// 处理风向数据
		$fx1 = str_replace ( array("风","向"), "", $this->weather_data['today']["wind_direction_1"] );
		$fx2 = str_replace ( array("风","向"), "", $this->weather_data['today']["wind_direction_2"] );
		// 处理风力数据
		$fl1 = str_replace ( array ( "小于", "级"), "", $this->weather_data['today']["wind_power_1"]);
		$fl2 = str_replace ( array ( "小于", "级"), "", $this->weather_data['today']["wind_power_2"]);
		if(false!==strpos($fl1, "-")){ $arr = explode("-", $fl1); $fl1 = $arr[0]; }
		if(false!==strpos($fl2, "-")){ $arr = explode("-", $fl2); $fl2 = $arr[0]; }
		if($fl1 == "微风" || $fl1 =="无持续风向") $fl1 = 1;
		if($fl2 == "微风" || $fl2 =="无持续风向") $fl2 = 1;
		$fx1 = $this->windDirectionMap[$fx1].$fl1;
		$fx2 = $this->windDirectionMap[$fx2].$fl2;		
		// 处理紫外线
		$zwx = 0;
		// 处理温度
		$sd = 0;
		// 处理温度
		$qw1 = intval($this->weather_data['today']["temperature_1"]);
		$qw2 = intval($this->weather_data['today']["temperature_2"]);
		// 处理天气
		$tq1 = $this->weather_data['today']["weather_1"];
		$tq1 = str_replace("，", "转", $tq1);
		if(false!==strpos($tq1, "转")){
			$arr = explode("转", $tq1);
			$tq1 = $arr[0];
		}
		$tq2 = $this->weather_data['today']["weather_2"];
		$tq2 = str_replace("，", "转", $tq2);
		if(false!==strpos($tq2, "转")){
			$arr = explode("转", $tq1);
			$tq2 = $arr[0];
		}
		$tq1 = $this->weatherDirectionMap[$tq1];
		$tq2 = $this->weatherDirectionMap[$tq2];
		// 组合天气代码
		$retarr[] = "d:{$rq}.w:{$tq1}.w:{$tq2}.c:{$qw1}.c:{$qw2}.s:{$fx1}.s:{$fx2}.zw:{$zwx}.hu:{$sd}.";
		
		/********************************************* 明天天气数据 *********************************************/
		// 处理日期
		$date = date("Y-"). $this->weather_data["update_time"]["month"]. "-". $this->weather_data["update_time"]["day"];
		$rq = date("d", dateadd("day", 1, strtotime($date))); 
		// 处理风向数据
		$fx1 = str_replace ( array("风","向"), "", $this->weather_data['tomorrow']["wind_direction_day"] );
		$fx2 = str_replace ( array("风","向"), "", $this->weather_data['tomorrow']["wind_direction_night"] );
		// 处理风力数据
		$fl1 = str_replace ( array ( "小于", "级"), "", $this->weather_data['tomorrow']["wind_power_day"]);
		$fl2 = str_replace ( array ( "小于", "级"), "", $this->weather_data['tomorrow']["wind_power_night"]);
		if(false!==strpos($fl1, "-")){ $arr = explode("-", $fl1); $fl1 = $arr[0]; }
		if(false!==strpos($fl2, "-")){ $arr = explode("-", $fl2); $fl2 = $arr[0]; }
		if($fl1 == "微风" || $fl1 =="无持续风向") $fl1 = 1;
		if($fl2 == "微风" || $fl2 =="无持续风向") $fl2 = 1;
		$fx1 = $this->windDirectionMap[$fx1].$fl1;
		$fx2 = $this->windDirectionMap[$fx2].$fl2;
		// 处理紫外线
		$zwx = 0;
		// 处理温度
		$sd = 0;
		// 处理温度
		$qw1 = intval($this->weather_data['tomorrow']["temperature_day"]);
		$qw2 = intval($this->weather_data['tomorrow']["temperature_night"]);
		// 处理天气
		$tq1 = $this->weather_data['tomorrow']["weather_day"];
		$tq1 = str_replace("，", "转", $tq1);
		if(false!==strpos($tq1, "转")){
			$arr = explode("转", $tq1);
			$tq1 = $arr[0];
		}
		$tq2 = $this->weather_data['tomorrow']["weather_night"];
		$tq2 = str_replace("，", "转", $tq2);
		if(false!==strpos($tq2, "转")){
			$arr = explode("转", $tq1);
			$tq2 = $arr[0];
		}
		$tq1 = $this->weatherDirectionMap[$tq1];
		$tq2 = $this->weatherDirectionMap[$tq2];
		// 组合天气代码
		$retarr[] = "d:{$rq}.w:{$tq1}.w:{$tq2}.c:{$qw1}.c:{$qw2}.s:{$fx1}.s:{$fx2}.zw:{$zwx}.hu:{$sd}.";
		
		/********************************************* 后天天气数据 *********************************************/
		// 处理日期
		$date = date("Y-"). $this->weather_data["update_time"]["month"]. "-". $this->weather_data["update_time"]["day"];
		$rq = date("d", dateadd("day", 2, strtotime($date)));
		// 处理风向数据
		$fx1 = str_replace ( array("风","向"), "", $this->weather_data['tomorrow']["wind_direction_day"] );
		$fx2 = str_replace ( array("风","向"), "", $this->weather_data['tomorrow']["wind_direction_night"] );
		// 处理风力数据
		$fl1 = str_replace ( array ( "小于", "级"), "", $this->weather_data['tomorrow']["wind_power_day"]);
		$fl2 = str_replace ( array ( "小于", "级"), "", $this->weather_data['tomorrow']["wind_power_night"]);
		if(false!==strpos($fl1, "-")){ $arr = explode("-", $fl1); $fl1 = $arr[0]; }
		if(false!==strpos($fl2, "-")){ $arr = explode("-", $fl2); $fl2 = $arr[0]; }
		if($fl1 == "微风" || $fl1 =="无持续风向") $fl1 = 1;
		if($fl2 == "微风" || $fl2 =="无持续风向") $fl2 = 1;
		$fx1 = $this->windDirectionMap[$fx1].$fl1;
		$fx2 = $this->windDirectionMap[$fx2].$fl2;
		// 处理紫外线
		$zwx = 0;
		// 处理温度
		$sd = 0;
		// 处理温度
		$qw1 = intval($this->weather_data['tomorrow']["temperature_day"]);
		$qw2 = intval($this->weather_data['tomorrow']["temperature_night"]);
		// 处理天气
		$tq1 = $this->weather_data['tomorrow']["weather_day"];
		$tq1 = str_replace("，", "转", $tq1);
		if(false!==strpos($tq1, "转")){
			$arr = explode("转", $tq1);
			$tq1 = $arr[0];
		}
		$tq2 = $this->weather_data['tomorrow']["weather_night"];
		$tq2 = str_replace("，", "转", $tq2);
		if(false!==strpos($tq2, "转")){
			$arr = explode("转", $tq1);
			$tq2 = $arr[0];
		}
		$tq1 = $this->weatherDirectionMap[$tq1];
		$tq2 = $this->weatherDirectionMap[$tq2];
		// 组合天气代码
		$retarr[] = "d:{$rq}.w:{$tq1}.w:{$tq2}.c:{$qw1}.c:{$qw2}.s:{$fx1}.s:{$fx2}.zw:{$zwx}.hu:{$sd}.";
		
		/********************************************* 4-7天天气数据 *********************************************/
		for($i=0; $i<sizeof($this->weather_data["day_7"]); $i++){
			// 处理日期
			$date = date("Y-"). $this->weather_data["update_time"]["month"]. "-". $this->weather_data["update_time"]["day"];
			$rq = date("d", dateadd("day", 3+$i, strtotime($date)));
			// 处理风向数据
			$fx1 = str_replace ( array("风","向"), "", $this->weather_data['day_7'][$i]["wind_direction_1"] );
			$fx2 = str_replace ( array("风","向"), "", $this->weather_data['day_7'][$i]["wind_direction_2"] );
			// 处理风力数据
			$fl1 = str_replace ( array ( "小于", "级"), "", $this->weather_data['day_7'][$i]["wind_power"]);
			if(false!==strpos($fl1, "-")){ $arr = explode("-", $fl1); $fl1 = $arr[0]; }
			if($fl1 == "微风" || $fl1 =="无持续风向") $fl1 = 1;
			$fx1 = $this->windDirectionMap[$fx1].$fl1;
			$fx2 = $fx1;
			// 处理紫外线
			$zwx = 0;
			// 处理温度
			$sd = 0;
			// 处理温度
			$qw1 = intval($this->weather_data['day_7'][$i]["temperature_1"]);
			$qw2 = intval($this->weather_data['day_7'][$i]["temperature_2"]);
			// 处理天气
			$tq1 = $this->weather_data['day_7'][$i]["weather"];
			if(false!==strpos($tq1, "转")){
				$arr = explode("转", $tq1);
				$tq1 = $arr[0];
			}
			$tq1 = $this->weatherDirectionMap[$tq1];
			$tq2 = $tq1;
			// 组合天气代码
			$retarr[] = "d:{$rq}.w:{$tq1}.w:{$tq2}.c:{$qw1}.c:{$qw2}.s:{$fx1}.s:{$fx2}.zw:{$zwx}.hu:{$sd}.";
		}
				
		//header("Content-Type: application/txt; charset=utf-8");
		$s = "<weather>".implode("<no>",$retarr)."</weather> update:". date("Y-m-d H:i:s");
		echo $s;
	}
	
}